package com.ce.pc;

import java.util.Stack;

public class MyPC {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MyPC myPC = new MyPC();
		Stack<String> stock = new Stack<String>();
		Product p = myPC.new Product(stock);
		Thread tp = new Thread(p);
		Thread tc1 = new Thread(myPC.new Consume(stock));
		Thread tc2 = new Thread(myPC.new Consume(stock));
		tp.start();
		tc1.start();
		tc2.start();
	}

	public class Product implements Runnable {
		Stack<String> stock;
		
		public Product(Stack<String> stock) {
			this.stock = stock;
		}

		public void run() {
			while (true) {
				stock.add(""+System.currentTimeMillis());
				System.out.println("Product---stack size:"+stock.size());
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	public class Consume implements Runnable {
		Stack<String> stock;
		
		public Consume(Stack<String> stock) {
			this.stock = stock;
		}
		
		public void run() {
			while (true) {
				if(!stock.isEmpty())
					stock.pop();
				System.out.println("Consume---stack size:"+stock.size());
				try {
					Thread.sleep(300);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
}
